<?php
//订单数据统计控制器
namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class StatisticsController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    protected $statusArr=array("未认证","已认证");

	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('order');

		

	}

		

	

	
	//订单数据统计
    public function index(){

		header('Content-Type:text/html;charset=utf-8');
		if((!empty(I('start')) && (!empty(I('end'))))){
			$start = I('start');
			$end = I('end');
			$this->assign('start',$start);
			$this->assign('end',$end);
			$startday = $start.' 00:00:00';
			$endday = $end.' 23:59:59';
		}else{
			//获取本日起始时间戳和结束时间戳(没有时间筛选默认取当天的)
			$startday = date("Y-m-d",time()).' 00:00:00';
			$endday = date("Y-m-d",time()).' 23:59:59';
		}
		$day_where['end_time'] = array('between',array($startday,$endday));
		$day_where['status']  = array('in',[4,5]);
		$day['count'] = $this->m->where($day_where)->count();//完成数
		$day['all'] = $this->m->where(['addtime'=>['between',[$startday,$endday]]])->count();//总数
		$day['pre'] = bcmul(bcdiv($day['count'],$day['all'],2),100,2).'%';//完成百分比
		$day['price'] = $this->m->where($day_where)->sum('total_price');//完成金额
		if(empty($day['price'])){
			$day['price']=0;
		}
		$day_qx['cancel_time'] = array('between',array($startday,$endday));
		$day_qx['status']  = array('eq',7);
		$day['reset'] = $this->m->where($day_qx)->count();//取消订单数
		$this->assign('day',$day);
		//获取本周起始时间戳和结束时间戳  
		$startweek = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
		$endweek = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
		$where['end_time'] = array('between',array($startweek,$endweek));
		$where['status']  = array('in',[4,5]);
		$week['count'] = $this->m->where($where)->count();//完成数
		$week['all'] = $this->m->where(['addtime'=>['between',[$startweek,$endweek]]])->count();//总数
		$week['pre'] = bcmul(bcdiv($week['count'],$week['all'],2),100,2).'%';//完成百分比
		$week['price'] = $this->m->where($where)->sum('total_price');//完成金额
		if(empty($week['price'])){
			$week['price']=0;
		}
		$qx_where['cancel_time'] = array('between',array($startweek,$endweek));
		$qx_where['status']  = array('eq',7);
		$week['reset'] = $this->m->where($qx_where)->count();//取消订单数
		$this->assign('week',$week);
		//获取本月起始时间戳和结束时间戳
		$startmonth = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
		$endmonth = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
		$month_where['end_time'] = array('between',array($startmonth,$endmonth));
		$month_where['status']  = array('in',[4,5]);
		$month['count'] = $this->m->where($month_where)->count();//完成数
		$month['all'] = $this->m->where(['addtime'=>['between',[$startmonth,$endmonth]]])->count();//总数
		$month['pre'] = bcmul(bcdiv($month['count'],$month['all'],2),100,2).'%';//完成百分比
		$month['price'] = $this->m->where($month_where)->sum('total_price');//完成金额
		if(empty($month['price'])){
			$month['price']=0;
		}
		$month_qx['cancel_time'] = array('between',array($startmonth,$endmonth));
		$month_qx['status']  = array('eq',7);
		$month['reset'] = $this->m->where($month_qx)->count();//取消订单数
		$this->assign('month',$month);
		echo $this->m->getLastsql();
		var_dump($weekorder);
		echo $startweek;
		// echo $endweek;die;
		// die;


		$this->display('index');

    }


	public function ajaxListInput(){

		header('Content-Type:text/html;charset=utf-8');

		$info = I('post.');

		$data[$info["fieldName"]] = $info["Val"];

		$data["id"] = $info["id"];

		if ($this->m->save($data)){

			echo "success";

		}else{

			echo "error";

		}

		exit;

	}

	

	

	

	

}